/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jdRegularizacionMovimientos.java
 *
 * Created on 15-nov-2010, 9:43:47
 */

package Ordenes;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import suministros.ejb30.TO.DetalleOrdenTO;
import suministros.ejb30.TO.RegularizacionMovimientoTO;

/**
 *
 * @author Luis_Angel
 */
public class jdRegularizacionMovimientos extends javax.swing.JDialog implements ActionListener{

    private List items_check;
    private List<RegularizacionMovimientoTO> items_selec;
    private List<RegularizacionMovimientoTO> items;
    private JButton jbAceptar,jbSalir;
    private String ruc_proveedor;
    private jifRegistrarOrdenCompra frm;
    /** Creates new form jdRegularizacionMovimientos */
    public jdRegularizacionMovimientos(java.awt.Frame parent, boolean modal, String ruc_proveedor, jifRegistrarOrdenCompra frm,List<RegularizacionMovimientoTO> items) {
        super(parent, modal);
         this.ruc_proveedor = ruc_proveedor;
         this.frm = frm;
         this.items=items;
        creaComponentes();
        try {
            cargaLista();
        } catch (Exception ex) {
            Logger.getLogger(jdRegularizacionMovimientos.class.getName()).log(Level.SEVERE, null, ex);
        }
        initComponents();
    }

      public void creaComponentes(){
        JPanel p1 = new JPanel();
        jbAceptar = new JButton("Guardar");
        jbSalir = new JButton("Cancelar");

        p1.add(jbAceptar);
        p1.add(jbSalir);
        add(p1, BorderLayout.SOUTH);

        jbAceptar.addActionListener(this);
        jbSalir.addActionListener(this);
    }


    public void cargaLista() throws Exception{
        
        JPanel panel_checks = new JPanel(new GridLayout(items.size()+1,1));
        items_check = new ArrayList(1);
        panel_checks.add(new JLabel("Suministro / Documento / Monto(Soles)"));
        for(RegularizacionMovimientoTO si : items){
            JCheckBox chk1 = new JCheckBox(si.getDesc_suministro()+" / " +  si.getDocumento() + " / " + si.getMonto().doubleValue());
            panel_checks.add(chk1);
            items_check.add(chk1);
        }
        add(panel_checks, BorderLayout.CENTER);
    }

    private void verificarDatos() throws Exception{
         List<DetalleOrdenTO> items_selected = new ArrayList(1);
         items_selec =  new ArrayList(1);
         int indice = 0;
         for(Object si : items_check){
            JCheckBox tmp = (JCheckBox) si;
            if(tmp.isSelected()){
                  boolean b=false;
                  items_selec.add(items.get(indice));
                  for (DetalleOrdenTO detalleOrdenTO : items_selected) {
                    if(detalleOrdenTO.getSuministro().equalsIgnoreCase(items.get(indice).getId_suministro())){
                        detalleOrdenTO.setValor(detalleOrdenTO.getValor().add(items.get(indice).getMonto()));
                        b=true;
                        break;
                    }
                  }
                  if(!b){
                        DetalleOrdenTO det = new DetalleOrdenTO();
                         det.setSuministro(items.get(indice).getId_suministro());
                         det.setDescripcion(items.get(indice).getDesc_suministro());
                         det.setCantidad(items.get(indice).getCantidad());
                         det.setValor(items.get(indice).getMonto());
                         det.setDescripcionValor(items.get(indice).getUnidad());
                        items_selected.add(det);
                  }
                 
             }
            indice++;
         }
         frm.agregaDetalles(items_selected,true,items_selec);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        pack();
    }// </editor-fold>//GEN-END:initComponents

   

    public void actionPerformed(ActionEvent e) {
       if(e.getSource().equals(this.jbAceptar)){
            try {
                this.verificarDatos();
                 this.dispose();
            } catch (Exception ex) {
                Logger.getLogger(jdRegularizacionMovimientos.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else if(e.getSource().equals(this.jbSalir)){
            this.dispose();
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
